var config = require('../../config.js')
var http = require('../../utils/httpHelper.js')
Page({
  data: {
    userInfo:{},
    isLogin:false,
    teacherId:0,
    teacher:{},
    allCourses:[],
    sumPrice:0,
    timer:{},
    coursePage:{
      current:1,
      pageSize:12,
			total:0
    }
  },
  onLoad:function(options){
    var that = this;
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!='') {
      wx.showShareMenu({
        withShareTicket:true,
        menus:['shareAppMessage','shareTimeline']
      })
      that.setData({
        isLogin:true,
        userInfo:userInfo
      })
    }else {
      wx.hideShareMenu();
      wx.setStorageSync('inviteId', options.inviteId);
    }
    wx.setNavigationBarTitle({
      title: options.teacherName+'-课程展示'
    })
    var teacherId = options.teacherId;
    that.getTeacherInfo(teacherId,function(teacher){
      that.setData({teacher:teacher});      
    })
    that.getCourses(teacherId,function(list){
      that.setData({
        teacherId:teacherId,
        allCourses:list
      });
    })
  },
  onShow:function(){
    let userInfo = wx.getStorageSync('user');
    if (userInfo!=null&&userInfo!=undefined&&userInfo!={}&&userInfo!='') {
      this.setData({
        isLogin:true,
        userInfo:userInfo
      })
    }
    if (!this.data.isLogin) {
      wx.hideShareMenu()
    }
  },
  onReachBottom(){
    if(this.timer != null){
      clearTimeout(this.timer)
    }
    this.timer = setTimeout(()=>{
      this.getMoreData();
    },1000)
  },
  getMoreData() {
    var that = this;
    that.data.coursePage.current++;
    that.getCourses(function(list){
      let yList = that.data.allCourses;
      yList = yList.concat(list);
      that.setData({allCourses:yList});
    })
  },
  getCourses:function(teacherId,callback){
    var that = this;
    http.httpPost(config.clubApi.getCourseList,{
      pageNo:that.data.coursePage.current,
      pageSize:that.data.coursePage.pageSize,
      teacherId:teacherId
    },function (res){
      if(res.code == '200'){
        var list = res.data.pageData;
        typeof callback == "function" && callback(list)
      }
    });
  },
  toDetail:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../course/detail/index?id='+id
    })
  },
  getTeacherInfo:function(teacherId,callback){
    var data = {id:teacherId}
    http.httpGet(config.clubApi.getTeacherInfo,data,function(res){
      if(res.code == '200'){
        var teacher = res.data;
        typeof callback == "function" && callback(teacher)
      }
    });
  },
  // 分享到朋友圈
  onShareTimeline() {
    var teacher = this.data.teacher;
    var url = '/pages/course/index?teacherId='+teacher.id+'&inviteId='+this.data.userInfo.userId+'&teacherName='+teacher.teacherName;
    return {
      title: teacher.teacherName,
      path: url,
      imageUrl: teacher.teacherHeadimgurl,
      desc: teacher.teacherIntroduction
    }
  },
  // 转发
  onShareAppMessage() {
    var teacher = this.data.teacher;
    var url = '/pages/course/index?teacherId='+teacher.id+'&inviteId='+this.data.userInfo.userId+'&teacherName='+teacher.teacherName;
    return {
      title: teacher.teacherName,
      path: url,
      imageUrl: teacher.teacherHeadimgurl,
      desc: teacher.teacherIntroduction
    }
  }
})